package com.my.magicApi.annotation.aop;

import com.my.magicApi.annotation.MagicAuthority;
import com.my.magicApi.annotation.enums.AuthorityGroupEnum;
import com.my.magicApi.annotation.enums.AuthorityTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Aspect
@Component
@Order(value = 1)
public class MagicAuthorityAop {
//    @Autowired
//    private ISysRoleService roleService;
//    @Autowired
//    private ISysDeptService iSysDeptService;

    /**
     * 关注Authority注解
     */
    @Pointcut("@annotation(com.my.magicApi.annotation.MagicAuthority)")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void authority(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        MagicAuthority annotation = signature.getMethod().getAnnotation(MagicAuthority.class);
        if (annotation != null){
            Object[] args = joinPoint.getArgs();
            //默认取参数第一个 JSONObject
            Query query = new Query();
            for (int i = 0; i < args.length; i++) {
                Object arg = args[i];
                if (arg instanceof Query){
                    query = (Query)arg;
                    break;
                }
            }
            //获取请求参数
//            Object data = jsonObject.get(CommonInfoConstants.DATA);
            //未获取到参数直接返回不做处理
            if (Objects.isNull(query)) {
                log.info("未获取到参数，不做处理！");
                return;
            }
            AuthorityTypeEnum authorityEnum = annotation.authorityType();
            AuthorityGroupEnum[] authorityGroupEnums = annotation.authorityGroup();
            packingAuth(query,authorityEnum,authorityGroupEnums);
        }
    }

    /**
     * 封装权限
     * @param query
     * @param authorityEnum
     * @param authorityGroupEnums
     */
    private void packingAuth(Query query, AuthorityTypeEnum authorityEnum, AuthorityGroupEnum[] authorityGroupEnums){
        switch (authorityEnum) {
            case QUERY:
                queryAuth(query,authorityGroupEnums);
                break;
            case ADD:
//                addAuth(jsonArray);
                break;
            default:
                log.error("未找到权限封装类型：type={}",authorityEnum);
                throw new IllegalStateException("Unexpected value: " + authorityEnum);
        }
    }

    /**
     * 封装查询权限字段封装
     * @param query
     * @param authorityGroupEnums
     */
    private void queryAuth(Query query, AuthorityGroupEnum[] authorityGroupEnums){
        //获取当前用户部门信息
//        CurrentUserDept currentUserDept = iSysDeptService.getCurrentUserDept();
//        if (currentUserDept == null){
//            throw new CustomException(1,"获取当前用户失败");
//        }
//        Long sysDeptId = currentUserDept.getDeptId();
//        if (sysDeptId == null){
//            throw new CustomException(2,"获取当前用户部门信息失败");
//        }
        // 登录用户，数据拥有者用户ID过滤
//        Long checkUserId = roleService.returnUserId(currentUserDept.getUserId());
//        JSONObject jsonObject;

//        for (int i = 0; i< jsonArray.size(); i++){
//            jsonObject = jsonArray.getJSONObject(i);
//            if (Arrays.stream(authorityGroupEnums).anyMatch(item ->AuthorityGroupEnum.CHECK_USER.equals(item))){
//                if (Objects.nonNull(checkUserId)){
//                    //网格员
//                    JSONObject match = new JSONObject();
//                    match.put(PersonInfoConstants.CHECK_USER_ID,checkUserId.toString());
//                    jsonObject.put("match",match);
//                }
//            }
//            if (Arrays.stream(authorityGroupEnums).anyMatch(item ->AuthorityGroupEnum.CHECK_STREET.equals(item))){
//                // 登录用户，部门数据权限过滤
//                JSONObject in = new JSONObject();
//                in.put(PersonInfoConstants.DATA_DEPT_ID,iSysDeptService.getChildDept(sysDeptId, true));
//                jsonObject.put("in",in);
//            }
//            }
//        }
    }

    /**
     * 新增权限字段封装
     * @param jsonArray
     */
//    private void addAuth(JSONArray jsonArray){
//        //获取当前用户部门信息
//        CurrentUserDept currentUserDept = iSysDeptService.getCurrentUserDept();
//        if (currentUserDept == null){
//            throw new CustomException(1,"获取当前用户失败");
//        }
//        Long sysDeptId = currentUserDept.getDeptId();
//        if (sysDeptId == null){
//            throw new CustomException(2,"获取当前用户部门信息失败");
//        }
//        JSONObject jsonObject;
//        for (int i = 0; i< jsonArray.size(); i++) {
//            jsonObject = jsonArray.getJSONObject(i);
//            //封装当前登录用户ID
//            jsonObject.put(PersonInfoConstants.CHECK_USER_ID,currentUserDept.getUserId());
//            //封装当前登录用户部门ID
//            jsonObject.put(PersonInfoConstants.DATA_DEPT_ID,currentUserDept.getDeptId());
//            //封装当前部门的祖籍层级
//            jsonObject.put(PersonInfoConstants.ANCESTORS,currentUserDept.getAncestors());
//        }
//    }
}
